package com.hk.springsecurity03.service;

import com.hk.springsecurity03.dao.UserEntityRepository;
import com.hk.springsecurity03.entity.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author by 何坤
 * @Classname SecurityService
 * @Description TODO
 * @Date 2019/11/28 16:23
 */
@Service
public class SecurityService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(SecurityService.class);

    @Autowired
    private UserEntityRepository userEntityRepository;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        UserEntity userDetails = userEntityRepository.findByPhone(s);
        if (userDetails == null) {
            throw new UsernameNotFoundException("此用户不存在");
        }
        logger.info(userDetails.toString());
        return userDetails;
    }
}
